package com.forestar.xzk.commongisutil.CoordinateRelationship;

import java.util.ArrayList;

public class CoordinateSystemHelper
  {
    public enum CoorType
      {
        WGS84LatLon, WebMector, CGCS2000LatLon, Beijing54LatLon, Xian80LatLon, Beijing543Zone, Beijing543ZoneN, Beijing546Zone, Beijing546ZoneN, Xian803Zone, Xian803ZoneN, Xian806Zone, Xian806ZoneN, WGS84GaussKruger3Zone, WGS84GaussKruger3ZoneN, WGS84GaussKruger6Zone, WGS84GaussKruger6ZoneN, CGCS20003Zone, CGCS20003ZoneN, CGCS20006Zone, CGCS20006ZoneN, Beijing_Local
      }

    private static final double MetersPerDegree = 111111;

    private static boolean coorConvert = false;

    private static ArrayList<String> systemList;

    private static ArrayList<String> typeList;

    private static ArrayList<String> typeWgs84List;

    private static ArrayList<String> typeWorldList;

    public static String GetCoordDescription(int nSRID)
      {
        String strCoorDes = "";
        if (nSRID == 3395 || nSRID == 900913 || nSRID == 3857 || nSRID == 102100 || nSRID == 102113 || nSRID == 54004)
          {
            strCoorDes = "世界墨卡托投影坐标系";
          } else if (nSRID == 4326)
          {
            strCoorDes = "WGS84坐标系 经纬度";
          } else if (nSRID == 4214)
          {
            strCoorDes = "北京1954坐标系 经纬度";
          } else if (nSRID == 4610)
          {
            strCoorDes = "西安1980坐标系 经纬度";
          } else if (nSRID == 4490)
          {
            strCoorDes = "国家2000坐标系 经纬度";
          } else if (nSRID > 2400 && nSRID < 2422)
          {
            strCoorDes = "北京54坐标系3度分带" + String.valueOf(nSRID - 2400 + 24) + "带(加带号)";
          } else if (nSRID > 2421 && nSRID < 2443)
          {
            strCoorDes = "北京54坐标系3度分带" + String.valueOf(nSRID - 2421 + 24) + "带(不加带号)";
          } else if (nSRID > 21412 && nSRID < 21424)
          {
            strCoorDes = "北京54坐标系6度分带" + String.valueOf(nSRID - 21412 + 12) + "带(加带号)";
          } else if (nSRID > 21452 && nSRID < 21464)
          {
            strCoorDes = "北京54坐标系6度分带" + String.valueOf(nSRID - 21452 + 12) + "带(不加带号)";
          } else if (nSRID > 2348 && nSRID < 2370)
          {
            strCoorDes = "西安80坐标系3度分带" + String.valueOf(nSRID - 2348 + 24) + "带(加带号)";
          } else if (nSRID > 2369 && nSRID < 2391)
          {
            strCoorDes = "西安80坐标系3度分带" + String.valueOf(nSRID - 2369 + 24) + "带(不加带号)";
          } else if (nSRID > 2326 && nSRID < 2338)
          {
            strCoorDes = "西安80坐标系6度分带" + String.valueOf(nSRID - 2326 + 12) + "带(加带号)";
          } else if (nSRID > 2337 && nSRID < 2349)
          {
            strCoorDes = "西安80坐标系6度分带" + String.valueOf(nSRID - 2337 + 12) + "带(不加带号)";
          } else if (nSRID > 4512 && nSRID < 4534)
          {
            strCoorDes = "国家2000坐标系3度分带" + String.valueOf(nSRID - 4512 + 24) + "带(加带号)";
          } else if (nSRID > 4533 && nSRID < 4555)
          {
            strCoorDes = "国家2000坐标系3度分带" + String.valueOf(nSRID - 4533 + 24) + "带(不加带号)";
          } else if (nSRID > 4490 && nSRID < 4502)
          {
            strCoorDes = "国家2000坐标系6度分带" + String.valueOf(nSRID - 4490 + 12) + "带(加带号)";
          } else if (nSRID > 4501 && nSRID < 4513)
          {
            strCoorDes = "国家2000坐标系6度分带" + String.valueOf(nSRID - 4501 + 12) + "带(不加带号)";
          } else if (nSRID > 1000022 && nSRID < 1000044)
          {
            strCoorDes = "WGS84高斯投影坐标系3度分带" + (nSRID - 1000022 + 24) + "带(加带号)";
          } else if (nSRID > 1000043 && nSRID < 1000065)
          {
            strCoorDes = "WGS84高斯投影坐标系3度分带" + (nSRID - 1000043 + 24) + "带(不加带号)";
          } else if (nSRID > 1000000 && nSRID < 1000012)
          {
            strCoorDes = "WGS84高斯投影坐标系6度分带" + (nSRID - 1000000 + 12) + "带(加带号)";
          } else if (nSRID > 1000011 && nSRID < 1000023)
          {
            strCoorDes = "WGS84高斯投影坐标系6度分带" + (nSRID - 1000011 + 12) + "带(不加带号)";
          } else if (nSRID == 1000000)
          {
            strCoorDes = "北京地方坐标系";
          } else
          {
            strCoorDes = "未知坐标系统";
          }
        return strCoorDes;
      }

    public static String[] getCoordDescriptionArray(int nSRID)
      {
        String[] describe = new String[3];
        String strCoorDes = "";
        if (nSRID == 3395 || nSRID == 900913 || nSRID == 3857 || nSRID == 102100 || nSRID == 102113 || nSRID == 54004)
          {
            strCoorDes = "世界墨卡托投影坐标系";
            describe[0] = strCoorDes;
            describe[1] = strCoorDes;
            describe[2] = strCoorDes;
          }
        if (nSRID == 4326)
          {
            strCoorDes = "WGS84坐标系 经纬度";
            describe[0] = "WGS84坐标系";
            describe[1] = "地理坐标系 经纬度";
            describe[2] = strCoorDes;
          } else if (nSRID == 4214)
          {
            strCoorDes = "北京1954坐标系 经纬度";
            describe[0] = "北京1954坐标系";
            describe[1] = "地理坐标系 经纬度";
            describe[2] = strCoorDes;
          } else if (nSRID == 4610)
          {
            strCoorDes = "西安1980坐标系 经纬度";
            describe[0] = "西安1980坐标系";
            describe[1] = "地理坐标系 经纬度";
            describe[2] = strCoorDes;
          } else if (nSRID > 2400 && nSRID < 2422)
          {
            strCoorDes = "北京54坐标系3度分带" + String.valueOf(nSRID - 2400 + 24) + "带(加带号)";
            describe[0] = "北京1954坐标系";
            describe[1] = "3度分带加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 2421 && nSRID < 2443)
          {
            strCoorDes = "北京54坐标系3度分带" + String.valueOf(nSRID - 2421 + 24) + "带(不加带号)";
            describe[0] = "北京1954坐标系";
            describe[1] = "3度分带不加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 21412 && nSRID < 21424)
          {
            strCoorDes = "北京54坐标系6度分带" + String.valueOf(nSRID - 21412 + 12) + "带(加带号)";
            describe[0] = "北京1954坐标系";
            describe[1] = "6度分带加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 21452 && nSRID < 21464)
          {
            strCoorDes = "北京54坐标系6度分带" + String.valueOf(nSRID - 21452 + 12) + "带(不加带号)";
            describe[0] = "北京1954坐标系";
            describe[1] = "6度分带不加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 2348 && nSRID < 2370)
          {
            strCoorDes = "西安80坐标系3度分带" + String.valueOf(nSRID - 2348 + 24) + "带(加带号)";
            describe[0] = "西安1980坐标系";
            describe[1] = "3度分带加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 2369 && nSRID < 2391)
          {
            strCoorDes = "西安80坐标系3度分带" + String.valueOf(nSRID - 2369 + 24) + "带(不加带号)";
            describe[0] = "西安1980坐标系";
            describe[1] = "3度分带不加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 2326 && nSRID < 2338)
          {
            strCoorDes = "西安80坐标系6度分带" + String.valueOf(nSRID - 2326 + 12) + "带(加带号)";
            describe[0] = "西安1980坐标系";
            describe[1] = "6度分带加带号";
            describe[2] = strCoorDes;
          } else if (nSRID > 2337 && nSRID < 2349)
          {
            strCoorDes = "西安80坐标系6度分带" + String.valueOf(nSRID - 2337 + 12) + "带(不加带号)";
            describe[0] = "西安1980坐标系";
            describe[1] = "6度分带不加带号";
            describe[2] = strCoorDes;
          }
        return describe;
      }

    public static int GetCoorSrid(String strDescription)
      {
        int nSRID = 0;
        String strZone = "";
        if (strDescription.equals("世界墨卡托投影坐标系"))
          {
            nSRID = 900913;
          }
        if (strDescription.equals("WGS84坐标系 经纬度"))
          {
            nSRID = 4326;
          } else if (strDescription.equals("北京1954坐标系 经纬度"))
          {
            nSRID = 4214;
          } else if (strDescription.equals("西安1980坐标系 经纬度"))
          {
            nSRID = 4610;
          } else if (strDescription.equals("国家2000坐标系 经纬度"))
          {
            nSRID = 4490;
          } else if (strDescription.equals("北京地方坐标系"))
          {
            nSRID = 1000000;
          } else if (strDescription.contains("北京54坐标系3度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 2400 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("北京54坐标系3度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 2421 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("北京54坐标系6度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 21412 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("北京54坐标系6度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 21452 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("西安80坐标系3度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 2348 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("西安80坐标系3度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 2369 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("西安80坐标系6度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 2326 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("西安80坐标系6度分带") && strDescription.contains("不加带号"))
          {
            strZone = strDescription.substring(11, 13);
            nSRID = 2337 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("国家2000坐标系3度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(13, 15);
            nSRID = 4512 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("国家2000坐标系3度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(13, 15);
            nSRID = 4533 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("国家2000坐标系6度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(13, 15);
            nSRID = 4490 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("国家2000坐标系6度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(13, 15);
            nSRID = 4501 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("WGS84高斯投影坐标系3度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(16, 18);
            nSRID = 1000022 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("WGS84高斯投影坐标系3度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(16, 18);
            nSRID = 1000043 + Integer.parseInt(strZone) - 24;
          } else if (strDescription.contains("WGS84高斯投影坐标系6度分带") && strDescription.contains("(加带号)"))
          {
            strZone = strDescription.substring(16, 18);
            nSRID = 1000000 + Integer.parseInt(strZone) - 12;
          } else if (strDescription.contains("WGS84高斯投影坐标系6度分带") && strDescription.contains("(不加带号)"))
          {
            strZone = strDescription.substring(16, 18);
            nSRID = 1000011 + Integer.parseInt(strZone) - 12;
          }
        return nSRID;
      }

    private static ArrayList<String> InitCoordSys(CoorType coorType)
      {
        ArrayList<String> list = new ArrayList<String>();
        String strCoorDes = "";
        switch (coorType)
          {
            case WGS84LatLon:
              list.add("WGS84坐标系 经纬度");
              break;
            case Beijing54LatLon:
              list.add("北京1954坐标系 经纬度");
              break;
            case Xian80LatLon:
              list.add("西安1980坐标系 经纬度");
              break;
            case CGCS2000LatLon:
              list.add("国家2000坐标系 经纬度");
              break;
            case WebMector:
              list.add("世界墨卡托投影坐标系");
              break;
            case Beijing_Local:
              list.add("北京地方坐标系");
              break;
            case Beijing543Zone:
            {
              for (int i = 1; i <= 21; i++)
                {
                  strCoorDes = "北京54坐标系3度分带" + String.valueOf(24 + i) + "带(加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Beijing543ZoneN:
            {
              for (int i = 1; i <= 21; i++)
                {
                  strCoorDes = "北京54坐标系3度分带" + String.valueOf(24 + i) + "带(不加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Beijing546Zone:
            {
              for (int i = 1; i <= 11; i++)
                {
                  strCoorDes = "北京54坐标系6度分带" + String.valueOf(12 + i) + "带(加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Beijing546ZoneN:
            {
              for (int i = 1; i <= 11; i++)
                {
                  strCoorDes = "北京54坐标系6度分带" + String.valueOf(12 + i) + "带(不加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Xian803Zone:
            {
              for (int i = 1; i <= 21; i++)
                {
                  strCoorDes = "西安80坐标系3度分带" + String.valueOf(24 + i) + "带(加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Xian803ZoneN:
            {
              for (int i = 1; i <= 21; i++)
                {
                  strCoorDes = "西安80坐标系3度分带" + String.valueOf(24 + i) + "带(不加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Xian806Zone:
            {
              for (int i = 1; i <= 11; i++)
                {
                  strCoorDes = "西安80坐标系6度分带" + String.valueOf(12 + i) + "带(加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case Xian806ZoneN:
            {
              for (int i = 1; i <= 11; i++)
                {
                  strCoorDes = "西安80坐标系6度分带" + String.valueOf(12 + i) + "带(不加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case CGCS20003Zone:
            {
              for (int i = 1; i <= 21; i++)
                {
                  strCoorDes = "国家2000坐标系3度分带" + String.valueOf(24 + i) + "(加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case CGCS20003ZoneN:
            {
              for (int i = 1; i <= 21; i++)
                {
                  strCoorDes = "国家2000坐标系3度分带" + String.valueOf(24 + i) + "(不加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case CGCS20006Zone:
            {
              for (int i = 1; i <= 11; i++)
                {
                  strCoorDes = "国家2000坐标系6度分带" + String.valueOf(12 + i) + "(加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            case CGCS20006ZoneN:
            {
              for (int i = 1; i <= 11; i++)
                {
                  strCoorDes = "国家2000坐标系6度分带" + String.valueOf(12 + i) + "(不加带号)";
                  list.add(strCoorDes);
                }
            }
            break;
            default:
              break;
          }
        return list;
      }

    public static ArrayList<String> getListBySystemAndType(String system, String type)
      {
        if (system == null || type == null)
          {
            throw new IllegalArgumentException("坐标系统选择为null");
          }
        CoorType coorType = null;
        if (system.equals("西安1980坐标系"))
          {
            if (type.equals("地理坐标系 经纬度"))
              {
                coorType = CoorType.Xian80LatLon;
              } else if (type.equals("3度分带加带号"))
              {
                coorType = CoorType.Xian803Zone;
              } else if (type.equals("6度分带加带号"))
              {
                coorType = CoorType.Xian806Zone;
              } else if (type.equals("3度分带不加带号"))
              {
                coorType = CoorType.Xian803ZoneN;
              } else if (type.equals("6度分带不加带号"))
              {
                coorType = CoorType.Xian806ZoneN;
              }
          } else if (system.equals("北京1954坐标系"))
          {
            if (type.equals("地理坐标系 经纬度"))
              {
                coorType = CoorType.Beijing54LatLon;
              } else if (type.equals("3度分带加带号"))
              {
                coorType = CoorType.Beijing543Zone;
              } else if (type.equals("6度分带加带号"))
              {
                coorType = CoorType.Beijing546Zone;
              } else if (type.equals("3度分带不加带号"))
              {
                coorType = CoorType.Beijing543ZoneN;
              } else if (type.equals("6度分带不加带号"))
              {
                coorType = CoorType.Beijing546ZoneN;
              }
          } else if (system.equals("WGS84坐标系"))
          {
            if (type.equals("地理坐标系 经纬度"))
              {
                coorType = CoorType.WGS84LatLon;
              }
          } else if (system.equals("世界墨卡托投影坐标系"))
          {
            if (type.equals("世界墨卡托投影坐标系"))
              {
                coorType = CoorType.WebMector;
              }
          } else if (system.equals("国家2000坐标系"))
          {
            if (type.equals("地理坐标系 经纬度"))
              {
                coorType = CoorType.CGCS2000LatLon;
              } else if (type.equals("3度分带加带号"))
              {
                coorType = CoorType.CGCS20003Zone;
              } else if (type.equals("6度分带加带号"))
              {
                coorType = CoorType.CGCS20006Zone;
              } else if (type.equals("3度分带不加带号"))
              {
                coorType = CoorType.CGCS20003ZoneN;
              } else if (type.equals("6度分带不加带号"))
              {
                coorType = CoorType.CGCS20006ZoneN;
              }
          } else if (system.equals("北京地方坐标系"))
          {
            if (type.equals("北京地方坐标系"))
              {
                coorType = CoorType.Beijing_Local;
              }
          }
        if (coorType != null)
          {
            return InitCoordSys(coorType);
          } else
          {
            return null;
          }

      }

    public static ArrayList<String> getListBySystem(String system)
      {
        if (system == null)
          {
            throw new IllegalArgumentException("坐标系统选择为null");
          }
        if (system.equals("西安1980坐标系"))
          {
            return getTypeList();
          } else if (system.equals("北京1954坐标系"))
          {
            return getTypeList();
          } else if (system.equals("WGS84坐标系"))
          {
            return getTypeWgs84List();
          } else if (system.equals("世界墨卡托投影坐标系"))
          {
            return getTypeWorldList();
          } else if (system.equals("国家2000坐标系"))
          {
            return getTypeList();
          } else if (system.equals("北京地方坐标系"))
          {
            return getTypeBeijingLocalList();
          } else
          {
            return null;
          }

      }

    public static ArrayList<String> getSystemList()
      {
        if (systemList == null)
          {
            systemList = new ArrayList<String>();
            systemList.add("西安1980坐标系");
            systemList.add("北京1954坐标系");
            systemList.add("WGS84坐标系");
            systemList.add("世界墨卡托投影坐标系");
            systemList.add("国家2000坐标系");
            systemList.add("北京地方坐标系");
          }
        return systemList;
      }

    private static ArrayList<String> getTypeList()
      {
        if (typeList == null)
          {
            typeList = new ArrayList<String>();
            typeList.add("地理坐标系 经纬度");
            typeList.add("3度分带加带号");
            typeList.add("6度分带加带号");
            typeList.add("3度分带不加带号");
            typeList.add("6度分带不加带号");
          }
        return typeList;
      }

    private static ArrayList<String> getTypeWgs84List()
      {
        if (typeWgs84List == null)
          {
            typeWgs84List = new ArrayList<String>();
            typeWgs84List.add("地理坐标系 经纬度");
          }
        return typeWgs84List;
      }

    private static ArrayList<String> getTypeWorldList()
      {
        if (typeWorldList == null)
          {
            typeWorldList = new ArrayList<String>();
            typeWorldList.add("世界墨卡托投影坐标系");
          }
        return typeWorldList;
      }

    private static ArrayList<String> getTypeBeijingLocalList()
      {
        if (typeWorldList == null)
          {
            typeWorldList = new ArrayList<String>();
            typeWorldList.add("北京地方坐标系");
          }
        return typeWorldList;
      }

    /**
     * < 默认地理坐标容差, 和ARCGIS相同
     */
    static final double DEFAULT_GEOGRAPHIC_XYTOLERANCE = 0.000000008983153;

    /**
     * 计算当前经度在所在的分带。
     *
     * @param centerX  经度
     * @param zoneWith 仅有两个值，3、6；3度分带还是6度分带。
     * @return
     */
    public static int getZoneCodebyZone(double centerX, int zoneWith)
      {
        int zoneCode = 0;
        if (zoneWith == 3)
          {
            zoneCode = (int) ((centerX - 1.5 + DEFAULT_GEOGRAPHIC_XYTOLERANCE) / zoneWith) + 1;
          } else if (zoneWith == 6)
          {
            zoneCode = (int) ((centerX + DEFAULT_GEOGRAPHIC_XYTOLERANCE) / zoneWith) + 1;
          }
        return zoneCode;
      }
  }
